package com.preskool.controller;

import com.github.pagehelper.PageHelper;
import com.preskool.dto.ActionResult;
import com.preskool.dto.TimeTableVo;
import com.preskool.po.TimeTable;
import com.preskool.service.TimeTableService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: lihao
 * @email: 2470849570@qq.com
 * @date: 20212021/1/8
 * @describe: 时间表的相关的控制层
 * @version: v1.0
 */
@Slf4j
@RestController
public class TimeTableController {

    //注入service层
    @Autowired
    private TimeTableService timeTableService;

    /**
     * 查询所有的TimeTable信息
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/findAllTime", method = RequestMethod.GET)
    public ActionResult findAllTime(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "5") Integer pageSize) {

        System.out.println(currentPage+"*****"+pageSize);
        List<TimeTableVo> all = timeTableService.findAll(currentPage, pageSize);
        //PageHelper.startPage(currentPage,pageSize);
        if (all != null) {
            for (TimeTableVo time : all) {
                log.info(all.toString());
            }
            return new ActionResult(200, "查询成功", all);
        } else {
            return new ActionResult(403, "操作被拒绝", null);
        }
    }

    /**
     * 根据ttid查询单TimeTableVo实体
     * @param ttid
     * @return
     */
    @RequestMapping(value = "/queryOne",method = RequestMethod.GET)
    public ActionResult queryOne(Integer ttid){
        TimeTableVo one = timeTableService.findOne(ttid);
        if (one != null){
            return new ActionResult(200,"查询成功",one);
        }else{
            return new ActionResult(406,"查询东西不存在",null);
        }
    }

    /**
     * 添加时间表数据
     * @param timeTable
     * @return
     */
    @RequestMapping(value = "/addTimeInfo",method = RequestMethod.PUT)
    public ActionResult addTimeInfo(@RequestBody TimeTable timeTable) {
        int i = timeTableService.addTimeTable(timeTable);
        if (i > 0) {
            return new ActionResult(200, "添加成功", null);
        } else {
            return new ActionResult(406,"无法完成添加操作",null);
        }
    }

    /**
     * 根据ttid删除数据
     * @param ttid
     * @return
     */
    @RequestMapping(value = "/deleteTimeInfo",method = RequestMethod.DELETE)
    public ActionResult deleteTimeInfo(Integer ttid){
        int i = timeTableService.deleteOne(ttid);
        if (i>0){
            return new ActionResult(200,"删除成功",null);
        }else{
            return new ActionResult(406,"删除操作失败",null);
        }
    }

    /**
     * 更新timeTable数据
     * @param timeTable
     * @return
     */
    @RequestMapping(value = "/updateTimeInfo",method = RequestMethod.POST)
    public ActionResult updateTimeInfo(@RequestBody TimeTable timeTable){
        System.out.println("222222");
        int i = timeTableService.updateTimeTable(timeTable);
        if (i>0){
            return new ActionResult(200,"更新操作成功",null);
        }else {
            return new ActionResult(406,"更新操作失败",null);
        }
    }

}
